package sunyu.tools.common;

import org.nutz.lang.Mirror;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @author 孙宇
 */
@Component
public class DumpTools {

    public String dumpPublicGetMethods(Object object) {
        if (null == object) {
            return "null";
        }
        StringBuilder sb = new StringBuilder();
        Mirror<?> mirror = Mirror.me(object.getClass());
        for (Method m : mirror.getMethods()) {
            if (Modifier.isPublic(m.getModifiers())) {
                if (m.getParameterTypes().length == 0) {
                    try {
                        sb.append(String.format("\n\t%10s : %s", m.getName(), m.invoke(object)));
                    } catch (Exception e) {
                        sb.append(String.format("\n\t%10s : %s", m.getName(), e.getMessage()));
                    }
                }
            }
        }
        return sb.toString();
    }

}
